/**
 * Copyright (C) 2012-2013 Dystopian Dreams
 * @author Danielle Obmann <alisiel.isalos@gmail.com>
 * 
 * 
 * This file is part of Grue.

    Grue is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Grue is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Grue.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.event.KeyEvent;


public class Grue extends javax.swing.JFrame 
{

    /**
     * Creates new form Grue
     */
    public Grue() 
    {
        theParty=new Party();
        theEncounter=new Encounter();
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        diaInit = new javax.swing.JDialog();
        txtInit = new javax.swing.JTextField();
        lblInit = new javax.swing.JLabel();
        btnSetInit = new javax.swing.JButton();
        btnCancelInit = new javax.swing.JButton();
        diaAddPC = new javax.swing.JDialog();
        btnAddPCOK = new javax.swing.JButton();
        btnAddPCCancel = new javax.swing.JButton();
        txtAddPC = new javax.swing.JTextField();
        lblAddPC = new javax.swing.JLabel();
        panMain = new javax.swing.JTabbedPane();
        panParty = new javax.swing.JPanel();
        btnAddPC = new javax.swing.JButton();
        btnRemovePC = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstParty = new javax.swing.JList();
        btnClearParty = new javax.swing.JButton();
        panCombat = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstEncounter = new javax.swing.JList();
        btnRollInit = new javax.swing.JButton();
        btnAddEncounter = new javax.swing.JButton();
        btnClearEncounter = new javax.swing.JButton();
        btnNextTurn = new javax.swing.JButton();
        btnDelay = new javax.swing.JButton();

        diaInit.setTitle("Initiative");
        diaInit.setAlwaysOnTop(true);
        diaInit.setBounds(new java.awt.Rectangle(0, 0, 300, 300));
        diaInit.addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
                diaInitLost(evt);
            }
        });

        txtInit.setText("INIT");
        txtInit.setToolTipText("");
        txtInit.setVerifyInputWhenFocusTarget(false);
        txtInit.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtInitKeyReleased(evt);
            }
        });

        lblInit.setText("Set Initiative");

        btnSetInit.setText("Set");
        btnSetInit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSetInitActionPerformed(evt);
            }
        });

        btnCancelInit.setText("Cancel");

        javax.swing.GroupLayout diaInitLayout = new javax.swing.GroupLayout(diaInit.getContentPane());
        diaInit.getContentPane().setLayout(diaInitLayout);
        diaInitLayout.setHorizontalGroup(
            diaInitLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(diaInitLayout.createSequentialGroup()
                .addGroup(diaInitLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(diaInitLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnSetInit)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelInit))
                    .addGroup(diaInitLayout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(txtInit, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(diaInitLayout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(lblInit)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        diaInitLayout.setVerticalGroup(
            diaInitLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(diaInitLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInit)
                .addGap(18, 18, 18)
                .addComponent(txtInit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(diaInitLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSetInit)
                    .addComponent(btnCancelInit))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        diaAddPC.setTitle("Add a PC");
        diaAddPC.setBounds(new java.awt.Rectangle(0, 0, 300, 300));

        btnAddPCOK.setText("OK");
        btnAddPCOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddPCOKActionPerformed(evt);
            }
        });

        btnAddPCCancel.setText("Cancel");
        btnAddPCCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddPCCancelActionPerformed(evt);
            }
        });

        txtAddPC.setToolTipText("");
        txtAddPC.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtAddPCKeyReleased(evt);
            }
        });

        lblAddPC.setText("Enter name of PC");

        javax.swing.GroupLayout diaAddPCLayout = new javax.swing.GroupLayout(diaAddPC.getContentPane());
        diaAddPC.getContentPane().setLayout(diaAddPCLayout);
        diaAddPCLayout.setHorizontalGroup(
            diaAddPCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(diaAddPCLayout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(diaAddPCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAddPC)
                    .addGroup(diaAddPCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(diaAddPCLayout.createSequentialGroup()
                            .addComponent(btnAddPCOK)
                            .addGap(101, 101, 101)
                            .addComponent(btnAddPCCancel))
                        .addComponent(txtAddPC, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        diaAddPCLayout.setVerticalGroup(
            diaAddPCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, diaAddPCLayout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(lblAddPC)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtAddPC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(diaAddPCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAddPCOK)
                    .addComponent(btnAddPCCancel))
                .addContainerGap(67, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Grue - Gaming Resources and Utilities for Everyone");

        btnAddPC.setText("Add PC");
        btnAddPC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddPCActionPerformed(evt);
            }
        });

        btnRemovePC.setText("Remove PC");
        btnRemovePC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemovePCActionPerformed(evt);
            }
        });

        lstParty.setModel(theParty.getList());
        jScrollPane1.setViewportView(lstParty);

        btnClearParty.setText("Clear");
        btnClearParty.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearPartyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panPartyLayout = new javax.swing.GroupLayout(panParty);
        panParty.setLayout(panPartyLayout);
        panPartyLayout.setHorizontalGroup(
            panPartyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panPartyLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(panPartyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAddPC)
                    .addComponent(btnRemovePC)
                    .addComponent(btnClearParty))
                .addContainerGap(313, Short.MAX_VALUE))
        );
        panPartyLayout.setVerticalGroup(
            panPartyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panPartyLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panPartyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panPartyLayout.createSequentialGroup()
                        .addComponent(btnAddPC)
                        .addGap(18, 18, 18)
                        .addComponent(btnRemovePC)
                        .addGap(18, 18, 18)
                        .addComponent(btnClearParty)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)))
        );

        panMain.addTab("Party", panParty);

        lstEncounter.setModel(theEncounter.getList());
        lstEncounter.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(lstEncounter);

        btnRollInit.setText("Roll Initiative");
        btnRollInit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRollInitActionPerformed(evt);
            }
        });

        btnAddEncounter.setText("Add Encounter");

        btnClearEncounter.setText("Clear Encounter");

        btnNextTurn.setText("Next Turn");
        btnNextTurn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextTurnActionPerformed(evt);
            }
        });

        btnDelay.setText("Delay/Return");

        javax.swing.GroupLayout panCombatLayout = new javax.swing.GroupLayout(panCombat);
        panCombat.setLayout(panCombatLayout);
        panCombatLayout.setHorizontalGroup(
            panCombatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panCombatLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(panCombatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnClearEncounter, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnNextTurn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnAddEncounter, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnRollInit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnDelay, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(260, Short.MAX_VALUE))
        );
        panCombatLayout.setVerticalGroup(
            panCombatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panCombatLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panCombatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panCombatLayout.createSequentialGroup()
                        .addComponent(btnRollInit)
                        .addGap(18, 18, 18)
                        .addComponent(btnAddEncounter)
                        .addGap(18, 18, 18)
                        .addComponent(btnClearEncounter)
                        .addGap(18, 18, 18)
                        .addComponent(btnNextTurn)
                        .addGap(18, 18, 18)
                        .addComponent(btnDelay)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 451, Short.MAX_VALUE))
                .addContainerGap())
        );

        panMain.addTab("Combat Tracker", panCombat);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panMain)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panMain)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddPCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddPCActionPerformed
        diaAddPC.setVisible(true);
        txtAddPC.selectAll();
    }//GEN-LAST:event_btnAddPCActionPerformed

    private void btnRollInitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRollInitActionPerformed
        String theName;
        String theText;
        
        theEncounter.updateParty(theParty);
        lstEncounter.setSelectedIndex(0);
        theName=theEncounter.theEncounter.get(0).getName();
        theText="Set initiative for "+theName;
        lblInit.setText(theText);
        diaInit.setVisible(true);
        txtInit.selectAll();
    }//GEN-LAST:event_btnRollInitActionPerformed

    private void btnSetInitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSetInitActionPerformed
        setInit();
    }//GEN-LAST:event_btnSetInitActionPerformed

    private void diaInitLost(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_diaInitLost
        int index=lstEncounter.getSelectedIndex();
        int range=theEncounter.theEncounter.size()-1;
        String theName;
        String theText;
        if((index==range)&&(theEncounter.theEncounter.get(index).getInit()>0))
        {
            diaInit.setAlwaysOnTop(false);
            diaInit.dispose();
            theEncounter.rollInitiative();
            lstEncounter.setSelectedIndex(0);
        }
            
        else
        {
        theName=theEncounter.theEncounter.get(index).getName();
        theText="Set initiative for "+theName;
        lblInit.setText(theText);
        diaInit.setVisible(true);
        txtInit.selectAll();
        }
    }//GEN-LAST:event_diaInitLost

    private void txtInitKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtInitKeyReleased
         if(evt.getKeyCode() == KeyEvent.VK_ENTER)
             setInit();
    }//GEN-LAST:event_txtInitKeyReleased

    private void btnAddPCOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddPCOKActionPerformed
        addPC();
    }//GEN-LAST:event_btnAddPCOKActionPerformed

    private void txtAddPCKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAddPCKeyReleased
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            addPC();
        }
        if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
        {
            diaAddPC.dispose();
        }
            
    }//GEN-LAST:event_txtAddPCKeyReleased

    private void btnAddPCCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddPCCancelActionPerformed
        diaAddPC.dispose();
    }//GEN-LAST:event_btnAddPCCancelActionPerformed

    private void btnNextTurnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextTurnActionPerformed
        int index=lstEncounter.getSelectedIndex();
        if (index==theEncounter.theEncounter.size()-1)
        {
            index=0;
        }
        else
        {
            index++;
        }
        lstEncounter.setSelectedIndex(index);
    }//GEN-LAST:event_btnNextTurnActionPerformed

    private void btnRemovePCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemovePCActionPerformed
        int index = lstParty.getSelectedIndex();
        theParty.removeMember(index);
        theEncounter.updateParty(theParty);
    }//GEN-LAST:event_btnRemovePCActionPerformed

    private void btnClearPartyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearPartyActionPerformed
        theParty.clearParty();
        theEncounter.updateParty(theParty);
    }//GEN-LAST:event_btnClearPartyActionPerformed

    private void setInit()
    {
        int theInt=lstEncounter.getSelectedIndex();
        Integer theInit=Integer.parseInt(txtInit.getText());
        theEncounter.theEncounter.get(theInt).setInit(theInit);
        theInt++;
        lstEncounter.setSelectedIndex(theInt);
        diaInit.dispose();
    }
    private void addPC()
    {
        theParty.addMember(txtAddPC.getText());
        theEncounter.updateParty(theParty);
        diaAddPC.dispose();
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) 
    {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try 
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) 
            {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } 
        catch (ClassNotFoundException ex) 
        {
            java.util.logging.Logger.getLogger(Grue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 
        catch (InstantiationException ex) 
        {
            java.util.logging.Logger.getLogger(Grue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 
        catch (IllegalAccessException ex) 
        {
            java.util.logging.Logger.getLogger(Grue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 
        catch (javax.swing.UnsupportedLookAndFeelException ex) 
        {
            java.util.logging.Logger.getLogger(Grue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                new Grue().setVisible(true);
            }
        });
    }
    
    private Party theParty;
    private Encounter theEncounter;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddEncounter;
    private javax.swing.JButton btnAddPC;
    private javax.swing.JButton btnAddPCCancel;
    private javax.swing.JButton btnAddPCOK;
    private javax.swing.JButton btnCancelInit;
    private javax.swing.JButton btnClearEncounter;
    private javax.swing.JButton btnClearParty;
    private javax.swing.JButton btnDelay;
    private javax.swing.JButton btnNextTurn;
    private javax.swing.JButton btnRemovePC;
    private javax.swing.JButton btnRollInit;
    private javax.swing.JButton btnSetInit;
    private javax.swing.JDialog diaAddPC;
    private javax.swing.JDialog diaInit;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblAddPC;
    private javax.swing.JLabel lblInit;
    private javax.swing.JList lstEncounter;
    private javax.swing.JList lstParty;
    private javax.swing.JPanel panCombat;
    private javax.swing.JTabbedPane panMain;
    private javax.swing.JPanel panParty;
    private javax.swing.JTextField txtAddPC;
    private javax.swing.JTextField txtInit;
    // End of variables declaration//GEN-END:variables
}
